<?php

namespace App\Repositories\Album;

use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;
use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use App\Models\Album\Album;
use App\Validators\AlbumValidator;
use Prettus\Repository\Traits\CacheableRepository;

/**
 * Class AlbumRepositoryEloquent.
 *
 * @package namespace App\Repositories;
 */
class AlbumRepositoryEloquent extends BaseRepository implements AlbumRepository
{

    use CacheableRepository;

    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return Album::class;
    }

    

    /**
     * Boot up the repository, pushing criteria
     */
    public function boot()
    {
        $this->pushCriteria(app(RequestCriteria::class));
    }

    /**
     * 返回指定id专辑
     * @author Jerry
     * @param int $id
     * @param int $isShow
     * @return mixed
     */
    public function findAlbumById(int $id, int $isShow)
    {
        $time = time();

        $where = [
            'id'    =>  $id,
            ['start_time', '<=', $time],
            ['end_time', '>', $time],
        ];

        if ($isShow) {
            $where = [
                'id'    =>  $id,
            ];
        }

        $expiresAt = Carbon::now()->addHour(3);
        return Cache::remember("album:albumInfo{$id}", $expiresAt, function () use ($where) {
            $result = $this->findWhere($where);
            return $result->first();
        });

    }
    
}
